import store from 'store';
import {VISITURL} from './env.js'
let _debounceTimeout = null,
	_throttleRunning = false

/**
 * 防抖
 * @param {Function} 执行函数
 * @param {Number} delay 延时ms   
 */
export const debounce = (fn, delay = 500) => {
	clearTimeout(_debounceTimeout);
	_debounceTimeout = setTimeout(() => {
		fn();
	}, delay);
}
/**
 * 节流
 * @param {Function} 执行函数
 * @param {Number} delay 延时ms  
 */
export const throttle = (fn, delay = 500) => {
	if (_throttleRunning) {
		return;
	}
	_throttleRunning = true;
	fn();
	setTimeout(() => {
		_throttleRunning = false;
	}, delay);
}
/**
 * toast
 */
export const msg = (title = '', param = {}) => {
	if (!title) return;
	uni.showToast({
		title,
		duration: param.duration || 1500,
		mask: param.mask || false,
		icon: param.icon || 'none'
	});
}
/**
 * 检查登录
 * @return {Boolean}
 */
export const isLogin = (options = {}) => {
	const token = uni.getStorageSync('uniIdToken');
	if (token) {
		return true;
	}
	if (options.nav !== false) {
		uni.navigateTo({
			url: '/pages/login/login'
		})
	}
	return false;
}
/**
 * 获取页面栈
 * @param {Number} preIndex为1时获取上一页
 * @return {Object} 
 */
export const prePage = (preIndex = 1) => {
	const pages = getCurrentPages();
	const prePage = pages[pages.length - (preIndex + 1)];

	return prePage.$vm;
}
/**
 * 格式化时间戳 Y-m-d H:i:s
 * @param {String} format Y-m-d H:i:s
 * @param {Number} timestamp 时间戳   
 * @return {String}
 */
export const date = (format, timeStamp) => {
	if ('' + timeStamp.length <= 10) {
		timeStamp = +timeStamp * 1000;
	} else {
		timeStamp = +timeStamp;
	}
	let _date = new Date(timeStamp),
		Y = _date.getFullYear(),
		m = _date.getMonth() + 1,
		d = _date.getDate(),
		H = _date.getHours(),
		i = _date.getMinutes(),
		s = _date.getSeconds();

	m = m < 10 ? '0' + m : m;
	d = d < 10 ? '0' + d : d;
	H = H < 10 ? '0' + H : H;
	i = i < 10 ? '0' + i : i;
	s = s < 10 ? '0' + s : s;

	return format.replace(/[YmdHis]/g, key => {
		return {
			Y,
			m,
			d,
			H,
			i,
			s
		} [key];
	});
}
//二维数组去重
export const getUnique = array => {
	let obj = {}
	return array.filter((item, index) => {
		let newItem = item + JSON.stringify(item)
		return obj.hasOwnProperty(newItem) ? false : obj[newItem] = true
	})
}
// 判断类型集合
export const checkStr = (str, type) => {
	switch (type) {
		case 'mobile': //手机号码
			return /^1[3|4|5|6|7|8|9][0-9]{9}$/.test(str);
		case 'tel': //座机
			return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?$/.test(str);
		case 'card': //身份证
			return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(str);
		case 'mobileCode': //6位数字验证码
			return /^[0-9]{6}$/.test(str)
		case 'pwd': //密码以字母开头，长度在6~18之间，只能包含字母、数字和下划线
			return /^([a-zA-Z0-9_]){6,18}$/.test(str)
		case 'payPwd': //支付密码 6位纯数字
			return /^[0-9]{6}$/.test(str)
		case 'postal': //邮政编码
			return /[1-9]\d{5}(?!\d)/.test(str);
		case 'QQ': //QQ号
			return /^[1-9][0-9]{4,9}$/.test(str);
		case 'email': //邮箱
			return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str);
		case 'money': //金额(小数点2位)
			return /^\d*(?:\.\d{0,2})?$/.test(str);
		case 'URL': //网址
			return /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(str)
		case 'IP': //IP
			return /((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))/.test(str);
		case 'date': //日期时间
			return /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2})(?:\:\d{2}|:(\d{2}):(\d{2}))$/.test(str) ||
				/^(\d{4})\-(\d{2})\-(\d{2})$/
				.test(str)
		case 'number': //数字
			return /^[0-9]$/.test(str);
		case 'english': //英文
			return /^[a-zA-Z]+$/.test(str);
		case 'chinese': //中文
			return /^[\\u4E00-\\u9FA5]+$/.test(str);
		case 'lower': //小写
			return /^[a-z]+$/.test(str);
		case 'upper': //大写
			return /^[A-Z]+$/.test(str);
		case 'HTML': //HTML标记
			return /<("[^"]*"|'[^']*'|[^'">])*>/.test(str);
		default:
			return true;
	}
}

/*
 *
 * 查询数据字典名称
 * dicData -数据字典的数据
 * dicCode -数据字典code
 * dicValue -数据字典值
 *
 */
export const queryDicName = (dicData, dicCode, dicValue) => {


	if (!dicCode) {
		return ''
	}
	if (!dicValue) {
		return ''
	}

	const data = dicData[dicCode] ? JSON.parse(dicData[dicCode]) : []
	if (!data.length) {
		return ''
	}
	const arr = data.filter(function(item) {
		return dicValue == item.CODE
	});
	if (arr.length) {
		return arr[0].CODE_NAME
	}
	return '';
}
/*
 *
 * 查询数据字典（多选）
 * dicdata -列表或详情接口返回的跟进人code
 * dicCode -filter.do接口返回的跟进人数据字典
 * dicValue
 */
export const queryDicNameMulti = (dicData, dicCode, dicValue) => {
	
	if (!dicCode) {
		return ''
	}
	if (!dicValue) {
		return ''
	}
	const data = dicData[dicCode] ? JSON.parse(dicData[dicCode]) : []
	const arr = []
	const codeArr = dicValue.split(',')
	data.filter((item, index) => {
		codeArr.includes(item.CODE) && arr.push(item.CODE_NAME)
	})
	
	return arr.join(',')
}

/*
 *
 * 查询跟进人
 * code -列表或详情接口返回的跟进人code
 * data -filter.do接口返回的跟进人数据字典
 *
 */
export const queryPersonName = (code, data) => {
	if (!code) {
		return ''
	}
	if (!data.length) {
		return ''
	}
	const arr = []
	const codeArr = code.split(',')
	data.filter((item, index) => {
		codeArr.includes(item.CODE) && arr.push(item.CODE_NAME)
	})
	return arr
}

//打开附件
export const openFile=(url) => {
	uni.showLoading({
		title:"保存文档中..."
	})
	uni.downloadFile({
		url: VISITURL+url,
		//url:VISITURL+'/pppzy/ppp/attach/download.do?dataKey=0108f5d586ad4d82bb0b5781561da147',
		//url:'http://www.wuhanxjy.cn/images/logo.png',
		success: function(res) {
			console.log('down',res)
			const tempFilePath = res.tempFilePath;
			// 保存文件
			uni.saveFile({
				tempFilePath,
				success: function(res) {
					console.log('save',res)
					const savedFilePath = res.savedFilePath;
					// 打开文件
					uni.hideLoading()
					uni.openDocument({
						filePath: savedFilePath,
						success: function(res) {
							console.log('res',res)
							console.log('打开文档成功')
						},
					});
				},
				fail: function(err) {
					uni.showToast({
						title:'文档保存失败',
						icon:'none'
					})
					uni.hideLoading()
					
					console.log('保存失败：', err)
				}
			});
		},
		fail: function(err) {
			console.log('下载失败：', err);
			uni.hideLoading()
			uni.showToast({
				title:'文档下载失败',
				icon:'none'
			})
		},
	});
}

//上传附件
export const upload=(name,tranType,fn)=>{
	
	uni.chooseMessageFile({
		 count: 1,
	  success (res) {
		  console.log(res,"123123123123")
	    const tempFilePaths = res.tempFiles;
		uni.showLoading({
			title:"附件上传中..."
		})
	    uni.uploadFile({
	      url: VISITURL+'/pppzy/ppp/attach/upload.do', //仅为示例，非真实的接口地址
	      filePath: tempFilePaths[0].path,
	      name: name,
	      formData: {
	        singleFileUploads: false,
			saveAttachImmediately:"true",
			recordRid:'',
			tranType:tranType,
			TT_TOKEN:uni.getStorageSync('token')
	      },
	      success (res){
			  console.log(res.data,"看这里")
	        const data = JSON.parse(res.data).files
	        //do something
			fn && fn(data)
			uni.hideLoading()
	      },
		  fail(){
			  uni.hideLoading()
		  }
	    })
	  }
	})
}